package com.heking.qlyopensdkapp.ui;

import android.content.Context;
import android.content.res.ColorStateList;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.heking.qlyopensdkapp.R;
import com.zhoug.common.adapters.recycler2.BaseViewHolder;

import androidx.core.content.ContextCompat;

/**
 * @Author: zhoug
 * @Date: 2024-11-06
 * @Description:
 */
public class QlyPlayMenuListAdapter extends MyBaseRecyclerViewAdapter<QlyPlayMenu> implements View.OnClickListener {
    private OnMenuClickListener onMenuClickListener;

    private int colorEnableTrue;
    private int colorEnableFalse;

    public QlyPlayMenuListAdapter(Context context) {
        super(context);
        colorEnableTrue = ContextCompat.getColor(context, R.color.textColor);
        colorEnableFalse = ContextCompat.getColor(context, R.color.textColorHint);
    }

    @Override
    protected int getLayoutId(int viewType) {
        return R.layout.qly_sdk_menu_item;
    }

    @Override
    protected void onBindData(BaseViewHolder holder, QlyPlayMenu data, int position, int viewType) {
        ImageView mIvMenu = holder.itemView.findViewById(R.id.iv_menu);
        TextView mTvMenu = holder.itemView.findViewById(R.id.tv_menu);
        if (data != null) {
            mIvMenu.setImageResource(data.getIcon());
            mTvMenu.setText(data.getName());
            if (data.isEnable()) {
                mIvMenu.setImageTintList(ColorStateList.valueOf(colorEnableTrue));
                mTvMenu.setTextColor(colorEnableTrue);
                holder.itemView.setClickable(true);
                holder.itemView.setOnClickListener(this);
            } else {
                mIvMenu.setImageTintList(ColorStateList.valueOf(colorEnableFalse));
                mTvMenu.setTextColor(colorEnableFalse);
                holder.itemView.setClickable(false);
            }
        }
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        int pos = (int) v.getTag();
        QlyPlayMenu itemData = getItemData(pos);
        if (onMenuClickListener != null && itemData != null) {
            onMenuClickListener.onMenuClick(pos, itemData);
        }
    }

    public void setOnMenuClickListener(OnMenuClickListener onMenuClickListener) {
        this.onMenuClickListener = onMenuClickListener;
    }

    public interface OnMenuClickListener {
        void onMenuClick(int pos, QlyPlayMenu itemData);
    }
}
